﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZK.BLL;
using ZK.BLL.Config;
using ZK.Entity;
using ZK.Entity.Config;
using ZK.MyControl;

namespace ZK.Client.WinForm
{
    public partial class FrmPrinter : Form
    {
        public FrmPrinter()
        {
            InitializeComponent();
        }
        BindingList<PrinterSetting> bindinglist = new BindingList<PrinterSetting>();
        BindingSource bindingSource = new BindingSource();

        private void FrmPrinter_Load(object sender, EventArgs e)
        {
            bindingSource.DataSource = bindinglist;
            dataView1.DataSource = bindingSource;
            AddCloumns();
            LoadData();
        }

        private void AddCloumns()
        {
  
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrinterSetting),nameof(PrinterSetting.PrinterName)));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrinterSetting), nameof(PrinterSetting.IPAddress)));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(PrinterSetting), nameof(PrinterSetting.Port)));
        }


        private void LoadData()
        {
           var list = PrinterSettingConfig.GetConfig().PrinterSettingList;
            bindinglist.Clear();

            if (list != null)
            {
                foreach (var item in list)
                {
                    bindinglist.Add(item);
                }
            }
          
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            bindinglist.Add(new PrinterSetting());
        }

 
       private void btnSave_Click(object sender, EventArgs e)
       {
         PrinterSettingConfig.GetConfig().PrinterSettingList = bindinglist.ToList();
        if (PrinterSettingConfig.GetConfig().EditConfig())
        {
            MessageBox.Show("保存成功");
        }
        else
        {
            MessageBox.Show("保存失败");
        }
    }



        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dataView1.CurrentCell != null && dataView1.CurrentCell.RowIndex > -1)
            {
                bindinglist.RemoveAt(dataView1.CurrentCell.RowIndex);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
